package com.sci99.ex.vm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import com.sci99.ex.model.Course;
import com.sci99.ex.model.Employee;
import com.sci99.ex.service.CourseAssignService;
import com.sci99.ex.service.CourseService;
import com.sci99.ex.service.EmpService;

public class EmployeeCourseAssignVM {
	Logger log = Logger.getLogger(EmployeeCourseAssignVM.class);
	private String department;
	private String name;
	private String course_name;
	private String course_code;
	private Employee selEmployee;
	/**
	 * 员工列表
	 */
	private List<Employee> employeeList;
	/**
	 * 分配窗口待分配课程列表
	 */
	private List<Course> courseList;
	/**
	 * 主界面已分配课程列表
	 */
	private List<Course> assignedList;
	/**
	 * 待分配课程列表选中项
	 */
	private Course selCourse;
	/**
	 * 已分配课程列表选中项
	 */
	private Course selAssignedCourse;

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Employee getSelEmployee() {
		return selEmployee;
	}

	public void setSelEmployee(Employee selEmployee) {
		this.selEmployee = selEmployee;
	}

	public List<Employee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<Employee> employeeList) {
		this.employeeList = employeeList;
	}

	public String getCourse_name() {
		return course_name;
	}

	public void setCourse_name(String course_name) {
		this.course_name = course_name;
	}

	public String getCourse_code() {
		return course_code;
	}

	public void setCourse_code(String course_code) {
		this.course_code = course_code;
	}

	public List<Course> getCourseList() {
		return courseList;
	}

	public void setCourseList(List<Course> courseList) {
		this.courseList = courseList;
	}

	public Course getSelCourse() {
		return selCourse;
	}

	public void setSelCourse(Course selCourse) {
		this.selCourse = selCourse;
	}

	public List<Course> getAssignedList() {
		return assignedList;
	}

	public void setAssignedList(List<Course> assignedList) {
		this.assignedList = assignedList;
	}

	public Course getSelAssignedCourse() {
		return selAssignedCourse;
	}

	public void setSelAssignedCourse(Course selAssignedCourse) {
		this.selAssignedCourse = selAssignedCourse;
	}

	@Command
	@NotifyChange("employeeList")
	public void search() {
		Map<String, Object> parameter = new HashMap<>(4, 1);
		parameter.put("DepartmentStr", department);
		parameter.put("Realname", name);
		try {
			employeeList = EmpService.getEmployee(parameter);
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
		}
	}

	@Command
	@NotifyChange("courseList")
	public void schCourse() {
		try {
			Map<String, Object> args = new HashMap<>(4, 1);
			args.put("name", course_name);
			args.put("code", course_code);
			courseList = CourseService.getCourseList(args);
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
		}
	}

     /**
     *
     */
    @Command
	@NotifyChange("assignedList")
	public void schSelCourse() {
		try {
			assignedList = CourseService.getCourseListByEmpId(selEmployee
					.getId());
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
		}
	}

	@Command
	public void assign(@BindingParam("win") Window win) {
		if (selEmployee == null) {
			Messagebox.show("请选择员工");
			return;
		}
		win.doModal();
	}

	@Command
	@NotifyChange("assignedList")
	public void addAssign(@BindingParam("win") Window win) {
		if (selCourse == null) {
			Messagebox.show("请选择课程");
			return;
		}
		try {
			CourseAssignService.addEmployeeCourseAssign(selCourse.getId(),
					selEmployee.getId());
			assignedList = CourseService.getCourseListByEmpId(selEmployee
					.getId());
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
			return;
		}
		Messagebox.show(Msg.OptOK);
		win.setVisible(false);
	}

	@Command
	@NotifyChange("assignedList")
	public void delAssign() {
		if (selAssignedCourse == null) {
			Messagebox.show("请选择课程");
			return;
		}
		
		if (Messagebox.show(Msg.CfmDel, null,
				Messagebox.OK + Messagebox.CANCEL, Messagebox.QUESTION) != Messagebox.OK)
			return;
		try {
			CourseAssignService.delEmployeeCourseAssign(
					selAssignedCourse.getId(), selEmployee.getId());
			assignedList = CourseService.getCourseListByEmpId(selEmployee
					.getId());
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
		}
	}

	@Command
	public void close(@BindingParam("win") Window win) {
		win.setVisible(false);
	}

}
